#include <stdio.h>

typedef unsigned char BYTE;
typedef unsigned char u8;

void CRC16(BYTE *Array, BYTE *Rcvbuf,unsigned int Len)
{
  unsigned int  IX,IY,CRC;
  CRC=0xFFFF;//set all 1 ////to detect leading 0's
  if (Len<=0)
    CRC = 0;
  else
    {
      Len--;
      for (IX=0;IX<=Len;IX++)
        {
          CRC=CRC^(unsigned int)(Array[IX]);            //
          for(IY=0;IY<=7;IY++)
            if ((CRC&1)!=0)	//最低位为1                 //LSB first
              CRC=(CRC>>1)^0xA001;                     //shift right !
            else
              CRC=CRC>>1;    //
        }
    }
  //Rcvbuf[0] = (CRC & 0xff00)>>8;//高位置
  //Rcvbuf[1] = (CRC & 0x00ff);  //低位置
  Rcvbuf[1] = (CRC & 0xff00)>>8;
  Rcvbuf[0] = (CRC & 0x00ff);  //低字节
}


int len = 80;
u8 s[80];
const G[17] = {1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1};

//idx = 0;
void compute(int idx)
{
  int i;

  for(i=idx;i<=len-17;i++)
    if(s[i])
      {
        idx = i;
        //the next position of compute
        for(i=0;i<17;i++)
          {
            s[idx+i] ^= G[i];
          }
        //continue ...
        compute(idx);
        return;
      }

  return;
  //if(i > len-16)                //no enough bits, finished
  //return ;                    
}







typedef unsigned char uchar;
typedef unsigned int uint;


/*
// CRC 码
uint crc16l(uchar *ptr,uchar len)
{
  uchar i;
  while(len--)
    {
      for(i=0x80; i!=0; i>>=1)
        {
          if((crc&0x8000)!=0) {crc<<=1; crc^=0x1021;}
          else crc<<=1;
          if((*ptr&i)!=0) crc^=0x1021;
        }
      ptr++;
    }
  return(crc);
}
*/


 /*
typedef unsigned short u16;
u16 crc16_byte(uchar *ptr, uchar len)
{
  u16 crc;
  crc >>= 8;            //the high byte is set to 0x00
  crc ^= crc[7..0] ^ d[7..0];
  
}
 */
  
uint crc16r(uchar *ptr,uchar len)               //shift right, LSB first
{
  //uchar i;
  uint i;
  uchar t;                      //temp value
  uchar *p = ptr;
  uint crc = 0;                 //init value
  
  while(len--)
    {
      for(i=0x01; i!=0x100; i<<=1)              //from LSB
        {
          /*
          if( crc & 0x1 ){
              crc >>= 1;
              crc ^= 0x8408;
          } else 
            crc >>= 1;

          if(*p & i)
            crc ^= 0x8408;
          */

          //this method is also right
          t = crc & 0x1;
          if(*p & i)
            t ^= 0x1;
          crc >>= 1;
          crc &= (~0x8000);             //set crc[15] to 0
          if(t)
            crc ^= 0x8408;              //reverse
        }
      p++;
    }
  return (crc & 0xffff);
}


//==================================================
// generate crc for debug (arbitrary bits)
//==================================================
typedef unsigned short u16;
static u16 crc16_regs;
void crc16r_init()
{
  crc16_regs = 0x0000;
}
//bit should be 0 or 1 (never other value)
void crc16r_input(uchar bit)
{
  uchar t;
  t = (crc16_regs & 0x0001) ^ bit;              //crc[0] ^ bit
  crc16_regs >>= 1;            //bit15 is 0 (or set crc &= (~0x8000))
  //crc ^= 0x8408*t;
  if(t)
    crc16_regs ^= 0x8408;      //else not change
}
void crc16r_test()
{
  uchar crcbuff[8] = {0xe3,0xd2,0x0d,0x06,0x00,0x00,0x00,0x00};
  int i, j;
  uint tmp;

  crc16r_init();
  for(i=0;i<8;i++)
    {
      for(j=0;j<8;j++)
        {
          crc16r_input( (crcbuff[i] >> j) & 0x1 );
        }
    }
/*
  //crc16r_input( 1 );          //CRC of one bit '1'
  tmp = 0x8408;
  tmp <<= 1;
  tmp |= 0x0001;
  crc16r_init();
  for(i=0;i<17;i++)
    crc16r_input( (tmp>>i) & 0x1 );
  */

  printf("==================================================\n");
  printf("CRC result        : 0x%04x\n", crc16_regs);
  printf("CRC from standard : 0x%04x\n", crc16r(crcbuff,8));
  printf("==================================================\n");
}
//==================================================



//====================================================================================================
// test byte error !!!
//====================================================================================================
void crc16r_input_byte(uchar byte)
{
  uchar t;
  int i;

  u16 u16tmp = 0x0000;
  //t = (crc16_regs & 0xff) ^ byte;               //crc[7..0] ^ byte[7..0]

  //////////
  t = crc16_regs ^ byte;                //low 8 bits
  crc16_regs >>= 8;
  for(i=0;i<8;i++)
    {
      //if(t[7-i])
      if(t & (0x80>>i))
        crc16_regs ^= (0x8408 >> i);
    }
  return;

  
  for(i=0;i<8;i++)
    {
      //if(t[7-i])
      if( (crc16_regs ^ byte) & (0x08 >> i) )
        u16tmp ^= (0x8408 >> i);
    }
  //  printf("factor : 0x%04x\n", u16t);
  crc16_regs >>= 8;
  crc16_regs ^= u16tmp;
}
void crc16r_byte_test()
{
  //uchar crcbuff[8] = {0xe3,0xd2,0x0d,0x06,0x00,0x00,0x00,0x00};
  uchar crcbuff[10] = {0xe3,0xd2,0x0d,0x06,0x00,0x00,0x00,0x00, 0x48, 0x50};
  int i, j;
  uint tmp;
  
  //  crc16r_init();
  //crc16_regs = 0xffff;
  crc16_regs = 0x0000;
  //for(i=0;i<10;i++)
  crcbuff[0] = 0x01;
  for(i=0;i<1;i++)
    {
      crc16r_input_byte(crcbuff[i]);
    }
  printf("CRC byte  : 0x%04x\n", crc16_regs);
  printf("right CRC : 0x%04x\n", crc16r(crcbuff,1));

}

//#define NUM_OF_BITS 2            //number of bits processed every time
void crc16r_nbits(u16 nbits, int NUM_OF_BITS)       //1-7, 8 is error
{
  u16 t;
  int i;
  
  t = crc16_regs ^ nbits;
  crc16_regs >>= NUM_OF_BITS;
  for(i=0;i<NUM_OF_BITS;i++)
    {
      //if(t[NUM_OF_BITS-1-i])
      if( (t >> (NUM_OF_BITS-1-i)) & 1)
        crc16_regs ^= (0x8408 >> i);
    }
}
void crc16r_nbits_test()
{
  uchar crcbuff[10] = {0xe3,0xd2,0x0d,0x06,0x00,0x00,0x00,0x00, 0x48, 0x50};
  int i, j;
  uint tmp;

  ///////////////////////////////////////////////////////////////////////////////
  // test while process 2 bits every time (NUM_OF_BITS = 2)
  ///////////////////////////////////////////////////////////////////////////////
  crc16r_init();
  crcbuff[0] = 0x01;
  for(i=0;i<4;i++)
    {
      crc16r_nbits(crcbuff[0] >> (i*2), 2);
    }
  printf("CRC nbits(every 2 bits) : 0x%04x\n", crc16_regs);
  ///////////////////////////////////////////////////////////////////////////////

  crc16r_init();
  crcbuff[0] = 0x01;
  for(i=0;i<8;i++)
    {
      crc16r_nbits(crcbuff[0] >> i*2, 1);
    }
  printf("CRC nbits(every 1 bits) : 0x%04x\n", crc16_regs);             //right

  
  crc16r_init();
  crcbuff[0] = 0x01;
  for(i=0;i<2;i++)
    {
      crc16r_nbits(crcbuff[0] >> i*4, 4);
    }
  printf("CRC nbits(every 4 bits) : 0x%04x\n", crc16_regs);             //right

  
  crc16r_init();
  crcbuff[0] = 0x01;
  for(i=0;i<1;i++)
    {
      crc16r_nbits(crcbuff[0], 8);
    }
  printf("CRC nbits(every 8 bits) : 0x%04x (error)\n", crc16_regs); //error (not valid for 8 bits)

  
  //printf("right CRC : 0x%04x\n", crc16r(crcbuff,1));

}


//the nibble_table (it is also the CRC of corresponding nibble) is generated by:
  /* { */
  /*   u16 xor; */
  /*   uint i,t; */
  /*   for(t=0;t<=0xf;t++) */
  /*     { */
  /*       xor = 0x0000; */
  /*       for(i=0;i<4;i++) */
  /*         { */
  /*           if( (t >> (3-i)) & 1) */
  /*             xor ^= (0x8408 >> i); */
  /*         } */
  /*       printf("0x%04x, ", xor); */
  /*     } */
  /*   printf("\n"); */
  /* } */
const u16 crc_nibble_table[16] = {0x0000, 0x1081, 0x2102, 0x3183, 0x4204, 0x5285, 0x6306, 0x7387, 0x8408, 0x9489, 0xa50a, 0xb58b, 0xc60c, 0xd68d, 0xe70e, 0xf78f};

void crc16r_nibble(uchar nibble)
{
  uchar t;
  int i;
  u16 xor;

  xor = crc_nibble_table[(crc16_regs ^ nibble) & 0xf];
  /*
  t = crc16_regs ^ nibble;
  for(i=0;i<4;i++)
    {
      if( (t >> (3-i)) & 1)
        xor ^= (0x8408 >> i);
    }
  */
  
  crc16_regs >>= 4;
  crc16_regs ^= xor;
}
void crc16r_nibble_test()
{
  uchar buf[] = {0x01, 0x02};
  int i;
  
  crc16r_init();
  for(i=0;i<2;i++)
    {
      crc16r_nibble(buf[0]>>i*4);
    }
  printf("CRC nibble : 0x%04x\n", crc16_regs);
}


const u16 crc_byte_table[] = {0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78};

void crc16r_byte_use_table(uchar byte)
{
  /*
    u16 xor;
  xor = crc_byte_table[(crc16_regs ^ byte) & 0xff];
  crc16_regs >>= 8;
  crc16_regs ^= xor;
  */
  crc16_regs = (crc16_regs >> 8) ^ crc_byte_table[(crc16_regs ^ byte) & 0xff];
}

void crc16r_byte_use_table_test()
{
  int i;
  uchar crcbuff[8] = {0xe3,0xd2,0x0d,0x06,0x00,0x00,0x00,0x00};
  
  crc16r_init();
  for(i=0;i<8;i++){
    crc16r_byte_use_table(crcbuff[i]);
  }
  printf("CRC byte use table : 0x%04x\n", crc16_regs);
}



//====================================================================================================
//CRC-CCITT (shift-right, LSB first) //not use crc table
u16 CRC_CCITT_r(uchar *dat, uint len)
{
  u16 crc = 0x0000;
  int i, j;

  for(i=0;i<len;i++)
    {
      crc ^= dat[i];
      for(j=0;j<8;j++)
        {
          if(crc & 1)
            crc = (crc >> 1) ^ 0x8408;
          else
            crc >>= 1;
        }
    }
  return crc;
}

//this version use crc table
u16 CRC_CCITT_table_r(uchar *dat, uint len)
{
  u16 crc = 0x0000;
  int i;
  for(i=0;i<len;i++)
    {
      crc = (crc >> 8) ^ crc_byte_table[(crc ^ dat[i]) & 0xff];
    }
  return crc;
}


//CRC32 
typedef unsigned int u32;

//this table is not right ?? (not verify!)
const u32 crc32_test_table[] ={0x00000000,0xdb710641,0x6d930ac3,0xb6e20c82,0xdb261586,0x005713c7,0xb6b51f45,0x6dc41904,0x6d3d2d4d,0xb64c2b0c,0x00ae278e,0xdbdf21cf,0xb61b38cb,0x6d6a3e8a,0xdb883208,0x00f93449,0xda7a5a9a,0x010b5cdb,0xb7e95059,0x6c985618,0x015c4f1c,0xda2d495d,0x6ccf45df,0xb7be439e,0xb74777d7,0x6c367196,0xdad47d14,0x01a57b55,0x6c616251,0xb7106410,0x01f26892,0xda836ed3,0x6f85b375,0xb4f4b534,0x0216b9b6,0xd967bff7,0xb4a3a6f3,0x6fd2a0b2,0xd930ac30,0x0241aa71,0x02b89e38,0xd9c99879,0x6f2b94fb,0xb45a92ba,0xd99e8bbe,0x02ef8dff,0xb40d817d,0x6f7c873c,0xb5ffe9ef,0x6e8eefae,0xd86ce32c,0x031de56d,0x6ed9fc69,0xb5a8fa28,0x034af6aa,0xd83bf0eb,0xd8c2c4a2,0x03b3c2e3,0xb551ce61,0x6e20c820,0x03e4d124,0xd895d765,0x6e77dbe7,0xb506dda6,0xdf0b66ea,0x047a60ab,0xb2986c29,0x69e96a68,0x042d736c,0xdf5c752d,0x69be79af,0xb2cf7fee,0xb2364ba7,0x69474de6,0xdfa54164,0x04d44725,0x69105e21,0xb2615860,0x048354e2,0xdff252a3,0x05713c70,0xde003a31,0x68e236b3,0xb39330f2,0xde5729f6,0x05262fb7,0xb3c42335,0x68b52574,0x684c113d,0xb33d177c,0x05df1bfe,0xdeae1dbf,0xb36a04bb,0x681b02fa,0xdef90e78,0x05880839,0xb08ed59f,0x6bffd3de,0xdd1ddf5c,0x066cd91d,0x6ba8c019,0xb0d9c658,0x063bcada,0xdd4acc9b,0xddb3f8d2,0x06c2fe93,0xb020f211,0x6b51f450,0x0695ed54,0xdde4eb15,0x6b06e797,0xb077e1d6,0x6af48f05,0xb1858944,0x076785c6,0xdc168387,0xb1d29a83,0x6aa39cc2,0xdc419040,0x07309601,0x07c9a248,0xdcb8a409,0x6a5aa88b,0xb12baeca,0xdcefb7ce,0x079eb18f,0xb17cbd0d,0x6a0dbb4c,0x6567cb95,0xbe16cdd4,0x08f4c156,0xd385c717,0xbe41de13,0x6530d852,0xd3d2d4d0,0x08a3d291,0x085ae6d8,0xd32be099,0x65c9ec1b,0xbeb8ea5a,0xd37cf35e,0x080df51f,0xbeeff99d,0x659effdc,0xbf1d910f,0x646c974e,0xd28e9bcc,0x09ff9d8d,0x643b8489,0xbf4a82c8,0x09a88e4a,0xd2d9880b,0xd220bc42,0x0951ba03,0xbfb3b681,0x64c2b0c0,0x0906a9c4,0xd277af85,0x6495a307,0xbfe4a546,0x0ae278e0,0xd1937ea1,0x67717223,0xbc007462,0xd1c46d66,0x0ab56b27,0xbc5767a5,0x672661e4,0x67df55ad,0xbcae53ec,0x0a4c5f6e,0xd13d592f,0xbcf9402b,0x6788466a,0xd16a4ae8,0x0a1b4ca9,0xd098227a,0x0be9243b,0xbd0b28b9,0x667a2ef8,0x0bbe37fc,0xd0cf31bd,0x662d3d3f,0xbd5c3b7e,0xbda50f37,0x66d40976,0xd03605f4,0x0b4703b5,0x66831ab1,0xbdf21cf0,0x0b101072,0xd0611633,0xba6cad7f,0x611dab3e,0xd7ffa7bc,0x0c8ea1fd,0x614ab8f9,0xba3bbeb8,0x0cd9b23a,0xd7a8b47b,0xd7518032,0x0c208673,0xbac28af1,0x61b38cb0,0x0c7795b4,0xd70693f5,0x61e49f77,0xba959936,0x6016f7e5,0xbb67f1a4,0x0d85fd26,0xd6f4fb67,0xbb30e263,0x6041e422,0xd6a3e8a0,0x0dd2eee1,0x0d2bdaa8,0xd65adce9,0x60b8d06b,0xbbc9d62a,0xd60dcf2e,0x0d7cc96f,0xbb9ec5ed,0x60efc3ac,0xd5e91e0a,0x0e98184b,0xb87a14c9,0x630b1288,0x0ecf0b8c,0xd5be0dcd,0x635c014f,0xb82d070e,0xb8d43347,0x63a53506,0xd5473984,0x0e363fc5,0x63f226c1,0xb8832080,0x0e612c02,0xd5102a43,0x0f934490,0xd4e242d1,0x62004e53,0xb9714812,0xd4b55116,0x0fc45757,0xb9265bd5,0x62575d94,0x62ae69dd,0xb9df6f9c,0x0f3d631e,0xd44c655f,0xb9887c5b,0x62f97a1a,0xd41b7698,0x0f6a70d9};
  //{0x00000000, 0xedb88320, 0xedb88320, 0x9b64c2b0, 0xedb88320, 0x9b64c2b0, 0x9b64c2b0, 0xa00ae278, 0xedb88320, 0x9b64c2b0, 0x9b64c2b0, 0xa00ae278, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0xedb88320, 0x9b64c2b0, 0x9b64c2b0, 0xa00ae278, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xedb88320, 0x9b64c2b0, 0x9b64c2b0, 0xa00ae278, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb6662d3d, 0xedb88320, 0x9b64c2b0, 0x9b64c2b0, 0xa00ae278, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb7bd5c3b, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb7bd5c3b, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb7bd5c3b, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb7bd5c3b, 0xb3667a2e, 0xb7bd5c3b, 0xb7bd5c3b, 0xb7bd5c3b, 0xedb88320, 0x9b64c2b0, 0x9b64c2b0, 0xa00ae278, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb40bbe37, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb40bbe37, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb40bbe37, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb40bbe37, 0xb3667a2e, 0xb40bbe37, 0xb40bbe37, 0xb7bd5c3b, 0x9b64c2b0, 0xa00ae278, 0xa00ae278, 0xbdbdf21c, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb40bbe37, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb40bbe37, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb40bbe37, 0xb3667a2e, 0xb40bbe37, 0xb40bbe37, 0xb40bbe37, 0xa00ae278, 0xbdbdf21c, 0xbdbdf21c, 0xb3667a2e, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb40bbe37, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb40bbe37, 0xb3667a2e, 0xb40bbe37, 0xb40bbe37, 0xb40bbe37, 0xbdbdf21c, 0xb3667a2e, 0xb3667a2e, 0xb40bbe37, 0xb3667a2e, 0xb40bbe37, 0xb40bbe37, 0xb40bbe37, 0xb3667a2e, 0xb40bbe37, 0xb40bbe37, 0xb40bbe37, 0xb40bbe37, 0xb40bbe37, 0xb40bbe37, 0xb40bbe37};

u32 CRC32_r(uchar *dat, u32 len)                //not right ??
{
  u32 crc = 0x00000000;
  int i;
  for(i=0;i<len;i++)
    {
      crc = (crc >> 8) ^ crc32_test_table[(crc ^ dat[i]) & 0xff];
    }
  return crc;
}




//this CRC32 method is right (verify ok) (MSB first)
const u32 crc32_table[] = {
            0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
            0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
            0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
            0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
            0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
            0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
            0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
            0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
            0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
            0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
            0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
            0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
            0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
            0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
            0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
            0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
            0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
            0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
            0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
            0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
            0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
            0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
            0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
            0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
            0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
            0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
            0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
            0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
            0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
            0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
            0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
            0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
};

u32 crc32(uchar *dat, unsigned int len)
{
    int i;
    u32 crc = 0xffffffff;               //to detect leading 0's
    for(i=0;i<len;i++)
        {
          crc = (crc>>8) ^ crc32_table[(crc^dat[i]) & 0xff];          //shift right ??
        }
    crc ^= 0xffffffff;                  //to detect trailing 0's
    return crc;
}
/*
int main()
{
    uchar buf[] = {0x01};
    printf("CRC32 : 0x%08x\n", crc32("hello", 5));         //the right value is 0x3610a686 (verify by python-zlib.crc32)

    printf("CRC32 : 0x%08x\n", crc32(buf, 1));
}
*/

 
//====================================================================================================

void main_old(void)
{
  //  uchar crcbuff [] = { 0x00,0x00,0x00,0x00,0x06,0x0d,0xd2,0xe3};                //for test
  uchar crcbuff[10] = {0xe3,0xd2,0x0d,0x06,0x00,0x00,0x00,0x00};        //total 8 bytes !!!!!
  uchar buf[10];
  int i;
  uint crc;
  
  crc = crc16r(crcbuff,8);              //shift right (LSB first)
  printf("Message CRC : 0x%04x\n", crc);

  crcbuff[8] = crc & 0xff;
  crcbuff[9] = (crc >> 8) & 0xff;
  crc = crc16r(crcbuff,10);
  printf("message with CRC info : 0x%04x\n", crc);

  
  crc16r_test();                        //test for arbitrary bits

  crc16r_byte_test();

  crc16r_nbits_test();

  crc16r_nibble_test();

  printf("==================================================\n");
  crc16r_byte_use_table_test();

  printf("==================================================\n");
  printf("CRC-CCITT                  : 0x%04x\n", CRC_CCITT_r(crcbuff, 8));
  printf("CRC-CCITT(use table)       : 0x%04x\n", CRC_CCITT_table_r(crcbuff, 8));
  printf("CRC-CCITT(contain CRC msg) : 0x%04x\n", CRC_CCITT_r(crcbuff, 10));
  
  {
    uchar bufx[] = {0x00, 0x00, 0x00, 0x01};
    printf("==================================================\n");
    printf("CRC-CCITT(no leading 0's): 0x%04x\n", CRC_CCITT_r(&bufx[3], 1));
    printf("CRC-CCITT(leading 0's)   : 0x%04x\n", CRC_CCITT_r(bufx, 4));                //they're the same result
  }

  printf("==================================================\n");
  printf("====== CRC32 reversing(not verify!)=====\n");
  //printf("CRC-32                     : 0x%08x\n", CRC32_r(crcbuff, 8));
  printf("CRC-32                     : 0x%08x\n", CRC32_r("hello", 5));
  {
    uchar buf[5+4] = {0x12,0x23,0x34,0x45,0x56};
    uint crc;
    crc = CRC32_r(buf, 5);
    printf("CRC-32    : 0x%08x\n", crc);
    buf[5] = crc & 0xff;
    buf[6] = (crc >> 8) & 0xff;
    buf[7] = (crc >> 16) & 0xff;
    buf[8] = (crc >> 24) & 0xff;
    crc = CRC32_r(buf, 9);
    printf("CRC-32(c) : 0x%08x\n", crc);
  }

  printf("==================================================\n");
  printf("====== CRC32 (verify ok)=====\n");
  printf("CRC32 : 0x%08x\n", crc32("hello", 5));         //the right value is 0x3610a686 (verify by python-zlib.crc32)
  {
    uchar buf1[] = {0x01};
    printf("CRC32(0x01) : 0x%08x\n", crc32(buf1, 1));
  }
  return 0;

  /*
  {
    u16 xor;
    uint i,t;
    for(t=0;t<=0xff;t++)
      {
        xor = 0x0000;
        for(i=0;i<8;i++)
          {
            if( (t >> (7-i)) & 1)
              xor ^= (0x8408 >> i);
          }
        printf("0x%04x, ", xor);
      }
    printf("\n");
  }
  */

  printf("generate CRC32 table ...\n");
  {
    uint c;
    uint POLY = 0xEDB88320;
    uint crc;
    for(c=0x00;c<=0xff;c++)
      {
        /*
        crc = crc ^ (c&0xff); //for c, only low byte is valid
        if(crc & 1) {
          crc >>= 1;
          crc ^= POLY;
        }else{
          crc >>= 1;
        }
        */
        crc = 0x00000000;
        for(i=0;i<8;i++)
          {
            if( (crc  ^ (c>>i)) & 1 )
              crc = (crc >> 1) ^ POLY;
            else
              crc >= 1;
          }
          printf("0x%08x, ", crc);
      }
  }


  return 0;

}


//----------------------------------------------------------------------------------------------------
// for ethernet MAC Layer CRC32, LSB first, shift left, print each step
unsigned int crc32_eth(unsigned char *buf, int len)
{
  int i, j;
  unsigned int CRC = 0xffffffff;
  for(i=0;i<len;i++)
    {
      unsigned char byte = buf[i];
      for(j=0;j<8;j++)
        {
          if((byte&0x1) ^ (CRC>>31))
            CRC = (CRC<<1) ^ 0x04c11db7;
          else
            CRC <<= 1;
          byte >>= 1;
        }
      printf("Data : %02x, CRC : %08x\n", buf[i], CRC);
    }
  // return ~CRC;
  return CRC;
}

int main()
{
  // 2 valid eth frame 
  /* unsigned char buf[] = {0x14,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3e,0x24,0xfc,0x2e}; */
  unsigned char buf[] = {0x55,0x55,0x55,0x55,0x55,0x55,0x55,0xD5,0x1,0x80,0xc2,0x0,0x0,0x1,0x4,0x1,0x2,0x3,0x4,0x50,0x88,0x8,0x0,0x2,0x12,0x13,0x14,0x15,0x11,0x12,0x13,0x14,0x30,0x0,0x3f,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xf9,0x4a,0xc6,0xc0};
  unsigned char eth_frame_without_crc[] = {0x14,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c};
  /* unsigned char buf[] = {0x10,0x11,0x12,0x13,0x14,0x15,0x20,0x21,0x22,0x23,0x24,0x25,0x08,0x00,0x45,0x00,0x00,0x2e,0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x00,0x01,0x92,0xdb,0xe9,0xb7,0xd0,0x9d,0xf6,0x71}; */

  printf("CRC32 for Ethernet (IEEE 802.3)\n====================\n");
  
  //if the eth frame contain the crc, then the result should be 0xc704dd7b !
  unsigned int crc = crc32_eth(buf, sizeof(buf));
  printf("====================\nthe CRC32 : 0x%08x", crc);
  if(crc == 0xc704dd7b)
    printf(" (check valid)");
  else
    printf(" (check error)");
  putchar('\n');
  return 0;
  //generate CRC for eth frame (corresponding IEEE 802.3, MSB in bytes, LSB in bits)
  {
    unsigned int crc = crc32_eth(eth_frame_without_crc, sizeof(eth_frame_without_crc));
    printf("====================\nthe CRC checksum appended to the frame(without CRC) is : \n");
    crc = ~crc;           //reverse
    // rolling the bits in each byte
    int i, j;
    for(j=24;j>=0;j-=8)       //24, 16, 8, 0
      {
        unsigned char byte = crc >> j;
        for(i=0;i<4;i++)
          {
            if( ((byte >> i) ^ (byte >> (7-i))) & 1 )
              byte ^= (0x80>>i) | (1<<i);
          }
        /* eth_frame_buf[index++] = byte; */
        printf("%02x ", byte);
      }
    printf("\n");
  }
  
  /* main_old(); */
}






